package com.xy.job.admin.core.notify

import com.xy.job.admin.domain.NotifyInstance
import com.xy.job.admin.enums.NotifyStatus
import com.xy.job.admin.repository.NotifyChannelConfigRepository
import org.springframework.beans.factory.getBean
import org.springframework.context.ApplicationContext
import org.springframework.stereotype.Component

@Component
class NotifySenderFacade(
    private var applicationContext: ApplicationContext,
    private var notifyChannelConfigRepository: NotifyChannelConfigRepository,
) {
    fun notify(notifyInstance: NotifyInstance): NotifyStatus {
        val notifyChannelConfig = notifyChannelConfigRepository.findById(notifyInstance.notifyConfigId!!).orElseThrow()
        val notifier: INotifySender = applicationContext.getBean(notifyChannelConfig.channelType!!.notifierClass)
        return notifier.notify(notifyInstance, notifyChannelConfig)
    }
}